const app=getApp()
Page({
    data:{
        nickName:'',
        avatarUrl:''
    },
    async getuserProfile(){
        let res= await wx.getUserProfile({
          desc: '用于小程序完善用户信息',
        })
        console.log(res)
        let {nickName,avatarUrl}=res.userInfo
        this.setData({
            nickName,avatarUrl
        })
    },
    async submit(e){
        console.log(e)
        let {sn,name}=e.detail.value
        if(!this.data.avatarUrl){
            wx.showToast({
              title: '请获取账号信息',
              icon:'error'
            })
            return
        }
        if(!sn|!name){
            wx.showToast({
              title: '请输入学号姓名',
              icon:'error'
            })
            return
        }
        if(!/^\d{8}$/.test(sn)){
            wx.showToast({
              title: '请输入8位学号',
              icon:'error'
            })
            return
        }
        let {nickName,avatarUrl}=this.data
        let res= await wx.cloud.callFunction({
            name:'pc_register',
            data:{
                sn,name,nickName,avatarUrl
            }
        })
        console.log(res)
        if(res.result.reg=='ok'){
            wx.showToast({
              title: '绑定成功',
            })
            app.globalData.user=res.result.user
            console.log("app",app.globalData.user)
            wx.navigateTo({
              url: '/pages/work18/rank',
            })
        }else{
            wx.showModal({
              cancelColor: 'cancelColor',
              icon:'err',
              title:res.result.reg,
              content:res.result.errMsg
            })
        }
    },
    async onLoad(){
      let res=await wx.cloud.callFunction({
        name:'pc_login'
      })
      if(res.result.result.name!='nobody'){
        app.globalData.user=res.result.result
        console.log(res)
        if(res.result.result.choosen.length>0){
          wx.redirectTo({
            url: '/pages/work18/list',
          })
        }else{
          wx.redirectTo({
            url: '/pages/work18/rank',
          })
        }
      }
    }
})